/*
 * 购物车数据模型构造
 */
import localStorage from '@/util/localstorage/index.js'

let cartName = ""
let cartList = []
let cartNum = 0

function FNcartModel() {
	return {
		$GuidId: 0,
		CommodityId: null, //商品ID
		CommodityName: null, //商品名称
		CommodityDesc: null, //商品优惠描述
		PackageId: null, //选中规格的ID
		PackageSupplyNo: null, //选中规格的sku
		PackageName: null, //选中规格名称
		Price: 0, //原价，固定不变
		ZkPrice: 0, //折扣后金额
		Discount: 0, //优惠的金额 = （原价-折扣金额）*数量
		Num: 0, //数量
		City: null, //城市
		DetailsList: [], //详细产品
		ExpireTime: null, //过期时间
		IsFlash: false, //是否支持闪送
		DeliveryCity: '',
		DeliveryStore: '',
		Checked: true,
		Size:''
	}
}

export default {
	cartName: 'shop_happy',
	getCartList(station = '上海') {
		let cartname = this.cartName
		let val = localStorage.getStorage(cartname) //获取数据
		let result = []
		this.cartNum = this.getCartNum(station)
		if (!val || val.length <= 0) return []
		val.map(item => {
			if (item.City == station) result.push(item)
		})

		return result;
	},
	add(val = {}, station = '上海') {
		let oldval = this.getCartList(station)
		let add = true
		oldval.map(item => {
			if (val.CommodityId == item.CommodityId && val.PackageId == item.PackageId && val.ZkPrice == item.ZkPrice && item.City ==
				station) {
				item.Num += val.Num || 1;
				add = false
			}
		})
		if (add) {
			//当前数组id
			let _$guidId = uuid(20, 16);
			let cartModel = new FNcartModel();
			cartModel.$GuidId = _$guidId;
			cartModel.CommodityId = val.CommodityId;
			cartModel.CommodityName = val.CommodityName;
			cartModel.CommodityDesc = val.CommodityDesc;
			cartModel.PackageId = val.PackageId;
			cartModel.PackageSupplyNo = val.PackageSupplyNo;
			cartModel.PackageName = val.PackageName;
			cartModel.Price = val.Price || 0;
			cartModel.ZkPrice = val.ZkPrice || 0;
			cartModel.Num = val.Num || 1;
			cartModel.Discount = (cartModel.Price - cartModel.ZkPrice) * cartModel.Num;
			cartModel.City = station;
			cartModel.DetailsList = val.DetailsList;
			cartModel.ExpireTime = val.ExpireTime;
			cartModel.IsFlash = val.IsFlash;
			cartModel.DeliveryCity = val.DeliveryCity;
			cartModel.DeliveryStore = val.DeliveryStore;
			cartModel.Checked = true;
			cartModel.Size=val.Size;
			oldval.push(cartModel)
		}

		localStorage.setStorage({
			key: this.cartName,
			jsonstr: oldval
		})
		this.cartNum = this.getCartNum(station)
		return oldval;
	},
	modify(t = 'del', item = {}, station = "上海") {
		let oldval = this.getCartList(station)
		let result = []
		if (t == 'clearall') {
			localStorage.removeStorage(this.cartName);
			this.getCartNum()
			return []
		}
		for (let a = 0; a < oldval.length; a++) {
			let old = oldval[a]
			if (t == 'del' && old.$GuidId != item.$GuidId) {
				result.push(old)
			} else if (old.$GuidId == item.$GuidId) {
				if (t == 'reduce') {
					if (old.Num > 1) {
						old.Num -= 1;
						result.push(old)
					}
				} else if (t == 'add') {
					old.Num += 1;
					result.push(old)
				}
				else if(t=='choose'){
					old.Checked = item.Checked;
					result.push(old)
				}
			} else {
				result.push(old)
			}
		}

		localStorage.setStorage({
			key: this.cartName,
			jsonstr: result
		})
		this.cartNum = this.getCartNum(station)
		return result
	},
	getCartNum(station = "上海") {
		let cartname = this.cartName
		let oldval = localStorage.getStorage(cartname) //获取数据
		cartNum = 0
		if (oldval)
			oldval.map(item => {
				cartNum += item.Num
			})
		if (cartNum > 0) {
			uni.setTabBarBadge({
				index: 2,
				text: cartNum.toString()
			})
		} else {
			uni.removeTabBarBadge({
				index: 2
			})
		}
		return cartNum
	},
	cartNum: 0

}

function uuid(len, radix) {
	var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
	var uuid = [],
		i;
	radix = radix || chars.length;

	if (len) {
		// Compact form
		for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
	} else {
		// rfc4122, version 4 form
		var r;

		// rfc4122 requires these characters
		uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
		uuid[14] = '4';

		// Fill in random data.  At i==19 set the high bits of clock sequence as
		// per rfc4122, sec. 4.1.5
		for (i = 0; i < 36; i++) {
			if (!uuid[i]) {
				r = 0 | Math.random() * 16;
				uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
			}
		}
	}

	return uuid.join('');
}
